package org.batch.service.batch.impl;

import cn.hutool.core.lang.Snowflake;
import org.batch.service.batch.BatchService;
import org.batch.utils.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 批处理相关操作接口实现
 *
 * @author LiKun
 * @date 2021/9/29 15:51
 */
@Service
public class BatchServiceImpl implements BatchService {
    public static final String PATH = "path";
    public static final String FIELDS = "fields";
    public static final String ACCOUNT = "account";
    private static final Logger logger = LoggerFactory.getLogger(BatchServiceImpl.class);

    @Resource
    private Job csvJob;
    @Resource
    private Job mysqlJob;
    @Resource
    private Snowflake snowflake;
    @Resource
    private SimpleJobLauncher simpleJobLauncher;

    public BatchServiceImpl() {
        logger.info("开始加载批处理相关操作接口实现类：BatchServiceImpl");
    }

    @Override
    public void batchDealTrading(String account) throws Exception {
        simpleJobLauncher.run(mysqlJob, ObjectUtil.jobParameters(snowflake.nextId(), ACCOUNT, account));
    }

    @Override
    public void batchTradingPersistence(String path, String fields) throws Exception {
        simpleJobLauncher.run(csvJob, ObjectUtil.jobParameters(snowflake.nextId(), PATH, path, FIELDS, fields));
    }
}
